<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SpiderController extends Controller
{
    //

    function getStockHistory(){
        set_time_limit(0);
        // $stocks = DB::table('stocks')->select('code')->where('id','>',1286)->get();
        $stocks = DB::table('stocks')->select('code')->get();
        $stocks->map(function($name){
            $startdate = DB::table('stock_data')->where('code',$name->code)->latest('stock_date')->value('stock_date');
            $url = 'http://img1.money.126.net/data/hs/kline/day/history/2017/1'.$name->code.'.json';
            $content=@file_get_contents($url);
           // echo 'xx';dd($content);
            $arr = [];
            if($content){
                $content = json_decode($content);
                if($content){
                    foreach($content->data as $key=>$val){
                        if($startdate<$val[0]){
                            $arr[$key]['code'] =$name->code;
                            $arr[$key]['open_price'] =$val[1];
                            $arr[$key]['close_price'] =$val[2];
                            $arr[$key]['high_price'] =$val[3];
                            $arr[$key]['low_price'] =$val[4];
                            $arr[$key]['volume'] =$val[5];
                            $arr[$key]['percent'] =$val[6];
                            $arr[$key]['state'] = ($val[6]>0 ? 1 : 2);
                            $arr[$key]['stock_date'] =$val[0];
                        }
                    }
                    if($arr){
                        DB::table('stock_data')->insert($arr);
                    }
                }else{
                    return '';
                }
                
            }else{
                $url = 'http://img1.money.126.net/data/hs/kline/day/history/2017/0'.$name->code.'.json';
                $content=@file_get_contents($url);
               // echo 'xaax';dd($content);
                if($content){
                    $content = json_decode($content);
                    foreach($content->data as $key=>$val){
                        if($startdate<$val[0]){
                            $arr[$key]['code'] =$name->code;
                            $arr[$key]['open_price'] =$val[1];
                            $arr[$key]['close_price'] =$val[2];
                            $arr[$key]['high_price'] =$val[3];
                            $arr[$key]['low_price'] =$val[4];
                            $arr[$key]['volume'] =$val[5];
                            $arr[$key]['percent'] =$val[6];
                            $arr[$key]['state'] = ($val[6]>0 ? 1 : 2);
                            $arr[$key]['stock_date'] =$val[0];
                        }
                    }
                    if($arr){
                        DB::table('stock_data')->insert($arr);
                    }
                }else{
                    return '';
                }

            }

          //  var_dump( $name->code);
        });
        
        echo '完成抓取';

    }

/*
    function getAllStock(){
        
        for($i=1;$i<42;$i++){
            $url = 'http://money.finance.sina.com.cn/d/api/openapi_proxy.php/?__s=[[%22hq%22,%22hs_a%22,%22%22,0,'.$i.',80]]';
            $content=file_get_contents($url);
            if(!$content) return '';
            $content = json_decode($content);
            $arr = [];
            foreach( $content[0]->items as $key=>$val){
                $arr[$key]['name'] =$val[2];
                $arr[$key]['code'] =$val[1];
            }
            // dd($arr);

            DB::table('stocks')->insert($arr);
        }
    }
    */
}
